package com.spring.api.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.api.web.service.WxEducationService;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.model.BaseResult;
import com.wechat.api.web.model.WxArticle;
import com.wechat.api.web.model.WxTeacher;
import com.wechat.api.web.model.WxVideo;

@Controller
@RequestMapping(value = "/wxEdu")
public class WxEducationController extends BaseController{
	
	@Resource WxEducationService wxEducationService;
	
	/**
	 * 根据业务类型及文章类型查询文章列表
	 * @param businessType
	 * @param articleType
	 * @param callback
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/wxArticle", method = RequestMethod.GET)
    public Object wxArticle(
    		@RequestParam(value="businessType",required = true) String businessType,
    		@RequestParam(value="articleType",required = true) String articleType,
    		@RequestParam(value="pageSize",required = true) Integer pageSize,
    		@RequestParam(value="pageIndex",required = true) Integer pageIndex,
    		HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,Object> parm = new HashMap<String,Object>();
			parm.put("businessType", businessType);
			parm.put("articleType", articleType);
			parm.put("pageSize", pageSize);
			parm.put("pageIndex", (pageIndex-1)*pageSize);
			List<WxArticle> list = wxEducationService.wxArticle(parm);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<WxArticle>();
		}
    }
	
	/**
	 * 根据业务类型查询业务类型详细内容
	 * @param businessType
	 * @param callback
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/wxBusinessType", method = RequestMethod.GET)
    public Object wxBusinessType(
    		@RequestParam(value="businessType",required = true) String businessType,
    		HttpServletRequest request,HttpServletResponse response){
		BaseResult ret = new BaseResult();
		try{
			Map<String,Object> parm = new HashMap<String,Object>();
			parm.put("businessType", businessType);
			ret.setDataset(wxEducationService.wxBusinessType(parm));
		}catch(Exception e){
			ret.setStatus("0").setMsg(e.getMessage());
			e.printStackTrace();
		}
		return buildResultInfo(ret);
    }
	
	/**
	 * 根据业务类型查找教师列表
	 * @param businessType
	 * @param callback
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/wxTeacher", method = RequestMethod.GET)
    public Object wxTeacher(
    		@RequestParam(value="businessType",required = true) String businessType,
    		@RequestParam(value="pageSize",required = true) Integer pageSize,
    		@RequestParam(value="pageIndex",required = true) Integer pageIndex,
    		HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,Object> parm = new HashMap<String,Object>();
			parm.put("businessType", businessType);
			parm.put("pageSize", pageSize);
			parm.put("pageIndex", (pageIndex-1)*pageSize);
			List<WxTeacher> list = wxEducationService.wxTeacher(parm);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<WxTeacher>();
		}
    }
	
	/**
	 * 根据业务类型查找教师详细信息
	 * @param businessType
	 * @param callback
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/wxTeacherInfo", method = RequestMethod.GET)
    public Object wxTeacherInfo(
    		@RequestParam(value="teacherId",required = true) String teacherId,
    		HttpServletRequest request,HttpServletResponse response){
		BaseResult ret = new BaseResult();
		try{
			Map<String,Object> parm = new HashMap<String,Object>();
			parm.put("teacherId", teacherId);
			WxTeacher retObj = wxEducationService.wxTeacherInfo(parm);
			if(retObj!=null){
				ret.setDataset(retObj);
			}else{
				ret.setStatus("0").setMsg("未找到该记录");
			}
		}catch(Exception e){
			ret.setStatus("0").setMsg("查询出错");
			e.printStackTrace();
		}
		return ret;
    }
	
	/**
	 * 根据业务类型查找视频资源列表
	 * @param businessType
	 * @param callback
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/wxVideo", method = RequestMethod.GET)
    public Object wxVideo(
    		@RequestParam(value="businessType",required = true) String businessType,
    		@RequestParam(value="pageSize",required = true) Integer pageSize,
    		@RequestParam(value="pageIndex",required = true) Integer pageIndex,
    		HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,Object> parm = new HashMap<String,Object>();
			parm.put("businessType", businessType);
			parm.put("pageSize", pageSize);
			parm.put("pageIndex", (pageIndex-1)*pageSize);
			List<WxVideo> list = wxEducationService.wxVideo(parm);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<WxVideo>();
		}
    }
}
